import java.util.Scanner;
public class day07 {
    private static class SNode {
        int data;
        SNode next;
        SNode(int data) {
            this.data = data;
            this.next = null;
        }
    }
    private static SNode top;

    public static void init() {
        top = null;
    }
    public static void push(int data) {
        SNode newNode = new SNode(data);
        newNode.next = top;
        top = newNode;
    }

    public static int pop() {
        if (isEmpty()) {
            System.out.println("栈为空！");
            return -1;
        }
        int data = top.data;
        top = top.next;
        return data;
    }

    public static int getTop() {
        return isEmpty() ? -1 : top.data;
    }

    public static boolean isEmpty() {
        return top == null;
    }

    public static void display() {
        if (isEmpty()) {
            System.out.println("空栈");
            return;
        }
        SNode current = top;
        System.out.print("栈元素：");
        while (current != null) {
            System.out.print(current.data + " ");
            current = current.next;
        }
        System.out.println();
    }
    public static void main(String[] args) {
        init();
        Scanner scanner = new Scanner(System.in);

        System.out.print("请输入要建立的栈中的元素个数：");
        int n = scanner.nextInt();
        for (int i = 1; i <= n; i++) {
            System.out.print("输入第" + i + "个入栈的元素：");
            int data = scanner.nextInt();
            push(data);
            System.out.println("当前栈顶元素：" + getTop());
            display();
        }
        System.out.print("输入一个要进栈的元素：");
        int newElem = scanner.nextInt();
        push(newElem);
        System.out.println("当前栈顶元素：" + getTop());
        display();

        System.out.println("执行一次出栈操作");
        pop();
        System.out.println("当前栈顶元素：" + getTop());
        display();

        scanner.close();
    }
}